/**
 * 禁用Enter键表单自动提交    
 */
 document.onkeydown = function (event) {
    var target, code, tag;
    if (!event) {
        event = window.event;
        //针对ie浏览器                  
        target = event.srcElement;
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "TEXTAREA") {
                return true;
            } else {
                return false;
            }
        }
    } else {
        target = event.target;
        //针对遵循w3c标准的浏览器，如Firefox                  
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") {
                return false;
            } else {
                return true;
            }
        }
    }
};
/**
 * 
 * @param {路由参数} name 
 * @returns 
 */
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return decodeURIComponent(r[2]);
    };
    return null;
}
function getUrlParams2(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
    context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}
/**
 * 
 * @param {关键字} key 
 * @param {值} val 
 * @returns 
 */
function setStorage(key, val) {
    return window.localStorage.setItem(key, val)
}
/**
 * 
 * @param {关键字} key 
 * @returns 
 */
function getStorage(key) {
    return window.localStorage.getItem(key)
}
/**
 * 
 * @param {关键字} key 
 * @returns 
 */
function removeStorage(key) {
    return window.localStorage.removeItem(key)
}
// 日期格式化
function add0(str) {
    // 根据 - 符号拆分
    return str
        .split("-")
        .map((item) => {
            // +item 将item字符串转换为数字
            // 小于10的时候就补全一个前缀0
            if (+item < 10) {
                return "0" + +item;
            }

            // 大于10的时候不用补0
            return item;
        })
        .join("-"); // 最后重组回来
}

function isAddZero(str) {
    return str
        .split(":")
        .map((item) => {
            // +item 将item字符串转换为数字
            // 小于10的时候就补全一个前缀0
            if (+item < 10) {
                return "0" + +item;
            }

            // 大于10的时候不用补0
            return item;
        })
        .join(":"); // 最后重组回来
}

function formatDateTime(date) {
    return add0(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} `) + " " + isAddZero(`${date.getHours()}:${date.getMinutes()}`);
}

function formatDate(date) {
    return add0(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`);
}
/**
 * 
 * @param {身份证号} IdCardNO 
 * @returns 30
 */
function getIdcardAge(IdCardNO) {
    var date = new Date();
    var yearfull = date.getFullYear();
    var age = ''
    if (IdCardNO.length == 18) //18位身份证号处理
    {
        var birthyear = IdCardNO.substring(6, 10);
        age = yearfull - birthyear;

    }
    if (IdCardNO.length == 15) //15位身份证号处理
    {
        var birthDay = "";
        birthDay = IdCardNO.substring(6, 12);
        birthDay = "19" + birthDay;
        birthDay = birthDay.substring(0, 4); // + "-" + birthDay.substring(4, 6) + "-" + birthDay.substring(6)
        age = yearfull - birthDay; //年龄
    }
    return age
}

/**
 * 
 * @param {身份证号} IdCardNO 
 * @returns  1992-08-13
 */
function getIdcardBirthday(IdCardNO) {
    let res = 0
    if (IdCardNO.length == 18) //18位身份证号处理
    {
        var birthDay = "";
        birthDay = IdCardNO.substring(6, 14);
        res = IdCardNO.substring(6, 10)+ "-" + IdCardNO.substring(10, 12) + "-" + IdCardNO.substring(12,14);

    }
    if (IdCardNO.length == 15) //15位身份证号处理
    {
        var birthDay = "";
        birthDay = IdCardNO.substring(6, 12);
        birthDay = "19" + birthDay;
        res = birthDay.substring(0, 4)+ "-" + birthDay.substring(4, 6) + "-" + birthDay.substring(6,8); // + "-" + birthDay.substring(4, 6) + "-" + birthDay.substring(6)
    }
    return res
}

// 日期转时间戳
function dateTotimestamp(date){
    return date.getTime();
}